package com.changgou.seckill.controller;

import com.changgou.common.constant.StatusCode;
import com.changgou.common.util.DateUtil;
import com.changgou.common.util.TokenDecode;
import com.changgou.common.vo.Result;
import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.seckill.service.SeckillGoodsService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/****
 * @Author:www.itheima.com
 * @Description:
 * @Date www.itheima.com
 *****/

@RestController
@RequestMapping("/seckill/goods")
@CrossOrigin
public class SeckillGoodsController {

    @Autowired
    private SeckillGoodsService seckillGoodsService;

    /**
    * 根据时间段、秒杀商品id查询在redis中的秒杀商品
    * @param time 时间段
    * @param id 秒杀商品id
    * @return 查询结果
    */
    @GetMapping("/one")
    public Result<SeckillGoods> findSeckillGoodsByTimeAndId(String time,Long id){
        SeckillGoods seckillGoods= seckillGoodsService.findSeckillGoodsByTimeAndId(time,id);
        return new Result<>("查询秒杀商品成功！",seckillGoods);
    }

    /**
     * 根据秒杀开始时间段查询该时间段内容的秒杀商品
     * @param time 秒杀开始时间段如：2020011212
     * @return
     */
    @GetMapping("/list")
    public Result<List<SeckillGoods>> list(String time){
        List<SeckillGoods> seckillGoodsList= seckillGoodsService.list(time);
        return new Result<>("查询时间段内秒杀商品成功！",seckillGoodsList);
    }

    /**
    * 获取5个最近的秒杀频道的时间段
    * @return 秒杀时间列表
    */
    @GetMapping("/menus")
    public Result<List<Date>> menus(){
        List<Date> dateMenusList = DateUtil.getDateMenus();
        return new Result<>("秒杀频道时间段查询成功！",dateMenusList);
    }

    /***
     * SeckillGoods分页条件搜索实现
     * @param seckillGoods: 查询条件
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<SeckillGoods>> findPage(@RequestBody(required = false)  SeckillGoods seckillGoods, @PathVariable  int page, @PathVariable  int size){
        //调用SeckillGoodsService实现分页条件查询SeckillGoods
        PageInfo<SeckillGoods> pageInfo = seckillGoodsService.findPage(seckillGoods, page, size);
        return new Result<PageInfo<SeckillGoods>>("查询成功！", pageInfo);
    }

    /***
     * SeckillGoods分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<SeckillGoods>> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用SeckillGoodsService实现分页查询SeckillGoods
        PageInfo<SeckillGoods> pageInfo = seckillGoodsService.findPage(page, size);
        return new Result<PageInfo<SeckillGoods>>("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param seckillGoods 查询条件
     * @return 查询结果
     */
    @PostMapping(value = "/search" )
    public Result<List<SeckillGoods>> findList(@RequestBody(required = false)  SeckillGoods seckillGoods){
        //调用SeckillGoodsService实现条件查询SeckillGoods
        List<SeckillGoods> list = seckillGoodsService.findList(seckillGoods);
        return new Result<List<SeckillGoods>>("查询成功！", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id 主键
     * @return 查询结果
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Long id){
        //调用SeckillGoodsService实现根据主键删除
        seckillGoodsService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功！");
    }

    /***
     * 修改SeckillGoods数据
     * @param seckillGoods 更新对象
     * @return 操作结果
     */
    @PutMapping
    public Result update(@RequestBody  SeckillGoods seckillGoods){
        //调用SeckillGoodsService实现修改SeckillGoods
        seckillGoodsService.update(seckillGoods);
        return new Result(true, StatusCode.OK, "修改成功！");
    }

    /***
     * 新增SeckillGoods数据
     * @param seckillGoods 新增对象
     * @return 操作结果
     */
    @PostMapping
    public Result add(@RequestBody  SeckillGoods seckillGoods){
        //调用SeckillGoodsService实现添加SeckillGoods
        seckillGoodsService.add(seckillGoods);
        return new Result(true, StatusCode.OK, "添加成功！");
    }

    /***
     * 根据ID查询SeckillGoods数据
     * @param id 主键
     * @return 查询结果
     */
    @GetMapping("/{id}")
    public Result<SeckillGoods> findById(@PathVariable Long id){
        //调用SeckillGoodsService实现根据主键查询SeckillGoods
        SeckillGoods seckillGoods = seckillGoodsService.findById(id);
        return new Result<SeckillGoods>("查询成功！", seckillGoods);
    }

    /***
     * 查询SeckillGoods全部数据
     * @return 查询结果
     */
    @GetMapping
    public Result<List<SeckillGoods>> findAll(){
        //调用SeckillGoodsService实现查询所有SeckillGoods
        List<SeckillGoods> list = seckillGoodsService.findAll();
        return new Result<List<SeckillGoods>>("查询成功！", list);
    }
}
